package com.chezai.client.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chezai.client.R;
import com.chezai.client.bean.DownloadProgress;
import com.chezai.client.global.DeviceFactory;
import com.chezai.client.global.TerminalApplication;
import com.chezai.client.update.AppUpdateManager;
import com.chezai.client.utils.AppManager;
import com.chezai.client.utils.PackageUtils;
import com.chezai.client.utils.SDCardUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 升级页
 * Created by likunyang on 2017/10/17.
 */

public class UpdateAppActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.download_progressBar)
    ProgressBar progressBar;
    @BindView(R.id.btn_cancel)
    Button btnCancel;

    public static void openUpdateApp(Activity from, String url) {
        if (AppManager.getAppManager().isOpenActivity(CameraActivity.class)) return;
        if (from != null) {
            Intent intent = new Intent(from, UpdateAppActivity.class);
            intent.putExtra("url", url);
            from.startActivity(intent);
        } else {
            Intent intent = new Intent(TerminalApplication.getTerminalContext(), UpdateAppActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            TerminalApplication.getTerminalContext().startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_update_app;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String[] split1 = url.split("/");
        String fileName = split1[split1.length - 1];
        AppUpdateManager.getInstance().download(url, SDCardUtil.getDownloadDir(), fileName);
    }

    @Override
    public void setListener() {
        btnCancel.setOnClickListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventDownloadProgress(DownloadProgress progress) {
        switch (progress.state) {
            case DownloadProgress.STATE_START:
                showToast("开始更新");
                break;
            case DownloadProgress.STATE_FAILE:
                btnCancel.setTag(DownloadProgress.STATE_FAILE);
                btnCancel.setText("下载失败，点击重试！");
                break;
            case DownloadProgress.STATE_LOADING:
                progressBar.setProgress((int) (progress.current / (progress.total * 1.0) * 100));
                break;
            case DownloadProgress.STATE_SUCESS:
                boolean isNew = PackageUtils.isNewVersionApk(this, progress.filePath);
                if (isNew) {
                    DeviceFactory.hardware.installApk(this, progress.filePath);
                    btnCancel.setText("正在安装");
                    showToast("更新完毕");
                } else {
                    showToast("更新的版本较旧");
                    finish();
                }
                break;
        }
        EventBus.getDefault().removeStickyEvent(progress);
    }

    @Override
    public void onClick(View v) {
        int tag = (int) (v.getTag() == null ? 0 : v.getTag());
        switch (tag) {
            case DownloadProgress.STATE_FAILE:
                AppUpdateManager.getInstance().reload();
                btnCancel.setText("下载中...");
                v.setTag(null);
                break;
            case DownloadProgress.STATE_CANCEL:
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}
